package com.qsmx.store.app.main;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.qsmx.store.app.R;
import com.qsmx.store.app.databinding.DelegatePasswordBinding;
import com.qsmx.store.core.fusion.FusionPre;
import com.qsmx.store.core.delegates.AtmosDelegate;
import com.qsmx.store.core.util.StringUtil;
import com.qsmx.store.core.util.storage.AtmosPreference;

import java.util.Objects;

public class PasswordDelegate extends AtmosDelegate {

    private DelegatePasswordBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_password;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegatePasswordBinding.bind(rootView);
        String password = StringUtil.getYearByTimestampLong(System.currentTimeMillis());
        AtmosPreference.addCustomStringPre(FusionPre.PASSWORD, password);

        mBinding.etPassword.setText(StringUtil.getYearByTimestampLong(System.currentTimeMillis()));
        mBinding.etPassword.requestFocus();

        onButtonClick();
    }

    private void onButtonClick() {
        mBinding.btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hideSoftKeyboard();
                if (Objects.requireNonNull(mBinding.etPassword.getText()).toString().equals(AtmosPreference.getCustomStringPre(FusionPre.PASSWORD))) {
                    getSupportDelegate().start(new AddressDelegate());
                } else {
                    showLongToast("密码错误");
                }
            }
        });

        mBinding.btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getProxyActivity().finish();
            }
        });

        mBinding.btClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearNums();
            }
        });

        mBinding.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                deleteOneNum();
            }
        });

        mBinding.btNum0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("0");
            }
        });

        mBinding.btNum1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("1");
            }
        });

        mBinding.btNum2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("2");
            }
        });

        mBinding.btNum3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("3");
            }
        });

        mBinding.btNum4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("4");
            }
        });

        mBinding.btNum5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("5");
            }
        });

        mBinding.btNum6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("6");
            }
        });

        mBinding.btNum7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("7");
            }
        });

        mBinding.btNum8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("8");
            }
        });

        mBinding.btNum9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                appendOneNum("9");
            }
        });
    }

    @SuppressLint("SetTextI18n")
    private void appendOneNum(String num) {
        if (mBinding.etPassword.hasFocus()) {
            mBinding.etPassword.setText(Objects.requireNonNull(mBinding.etPassword.getText()).toString() + num);
        }
    }

    private void clearNums() {
        if (mBinding.etPassword.hasFocus()) {
            mBinding.etPassword.setText("");
        }
    }

    private void deleteOneNum() {
        if (mBinding.etPassword.hasFocus()) {
            String phoneStr = Objects.requireNonNull(mBinding.etPassword.getText()).toString();
            if (!StringUtil.isEmpty(phoneStr)) {
                mBinding.etPassword.setText(phoneStr.substring(0, phoneStr.length() - 1));
            }
        }
    }
}
